package org.example.service;

import org.example.entity.Cart;
import org.example.mapper.ICartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CartService {

    @Autowired
    private ICartMapper cartMapper;

    // 1. 查询购物车信息
    public List<Cart> listCart(String userId, Integer businessId) {
        if (businessId == null) {
            return cartMapper.selectByUserId(userId);
        } else {
            return cartMapper.selectByUserIdAndBusinessId(userId, businessId);
        }
    }

    // 2. 添加购物车信息
    public int saveCart(String userId, Integer businessId, Integer foodId) {
        return cartMapper.insertCart(userId, businessId, foodId);
    }

    // 3. 更新购物车数量
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) {
        return cartMapper.updateCartQuantity(userId, businessId, foodId, quantity);
    }

    // 4. 删除购物车信息
    public int removeCart(String userId, Integer businessId, Integer foodId) {
        if (foodId == null) {
            return cartMapper.deleteByUserIdAndBusinessId(userId, businessId);
        } else {
            return cartMapper.deleteByUserIdBusinessIdAndFoodId(userId, businessId, foodId);
        }
    }
} 